package com.kakavr.order.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.order.service.IPurchaseHistoryService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.DateTimeUtils;
import com.kakavr.util.SessionUtils;
/**
 * 平台消费报表
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("orderReport")
public class OrderReportController extends BaseController {
	@Resource
	private IPurchaseHistoryService purchaseHistoryService;
	
	/**
	 * 统计游戏玩的次数
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectGameCountList", method = RequestMethod.GET)
	public Map<String, Object> selectGameCountList(HttpServletRequest request) {
		String type = request.getParameter("type");
		String begin = request.getParameter("begin");
		String end = request.getParameter("end");
		String storeId = request.getParameter("storeId");
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("partnerId", SessionUtils.getPartnerId());
		Date time = DateTime.now().toDate();
		if(StringUtils.isNotBlank(begin)){
			map.put("begin", begin);
		}
		if(StringUtils.isNotBlank(end)){
			map.put("end", end);
		}
		if(StringUtils.isNotBlank(storeId)){
			map.put("storeId", storeId);
		}
		if("week".equals(type)){
			map.put("begin", DateTimeUtils.getThisWeekMonday(time));
		}
		map.put("time", time);
		map.put("type", type);
		return MsgResult.okResultData(purchaseHistoryService
				.selectGameHistoryList(map));
	}
	
	/**
	 * 消费列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectOrderList", method = RequestMethod.GET)
	public Map<String, Object> selectOrderList(HttpServletRequest request){
		String storeId = request.getParameter("storeId");
		String name = request.getParameter("storeName");
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("partnerId", SessionUtils.getPartnerId());
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		if(StringUtils.isNotBlank(storeId)){
			map.put("storeId", storeId);
		}
		if(StringUtils.isNotBlank(name)){
			map.put("name", name);
		}
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseListByPage(map));
	}
	
	/**
	 * 消费详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectOrderInfo", method = RequestMethod.GET)
	public Map<String, Object> selectOrderInfo(HttpServletRequest request) {
		String purchaseId = request.getParameter("purchaseId");
		if (StringUtils.isBlank(purchaseId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("purchaseId", purchaseId);
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseInfo(map));
	}

}
